package com.mju.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mju.controller.common.JsonResult;
import com.mju.dao.RefundInfoDao;
import com.mju.feign.clients.OrderClient;
import com.mju.pojo.RefundInfo;
import com.mju.pojo.vo.OrderVo;
import com.mju.service.IRefundInfoService;
import com.mju.utils.GetTheCurrentTimeUtils;
import com.mju.utils.RefundNoUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class RefundInfoServiceImpl implements IRefundInfoService {
  @Autowired private RefundInfoDao refundInfoDao;
  @Autowired private OrderClient orderClient;



  @SneakyThrows
  @Override
  public RefundInfo createrRefundByOrder(Integer oid, String reason) {
    JsonResult<List<OrderVo>> data = orderClient.queryOrderByOid(oid);
    List<OrderVo> orderVos = data.getData();
    double totalPrice = 0L;

    if (!Objects.isNull(orderVos)){
      totalPrice=orderVos.get(0).getTotalPrice();
    }
    // 订单总价格
    double totalFee = new BigDecimal(totalPrice).doubleValue();
    // 订单号
    String orderNo = String.valueOf(oid);

    // 创建时间
    Date createTime = GetTheCurrentTimeUtils.getTheCurrentTime();

    // 生成退款信息
    RefundInfo refundInfo = new RefundInfo();

    refundInfo.setOrderNo(orderNo);
    // 退款单号
    refundInfo.setRefundNo(RefundNoUtils.getRefundNo());

    // 退款总金额
    refundInfo.setTotalFee(totalFee);
    // 退款金额，假设全退
    refundInfo.setRefund(totalFee);
    // 退款原因
    refundInfo.setReason(reason);
    // 创建退款信息时间
    refundInfo.setCreateTime(createTime);

    refundInfoDao.insert(refundInfo);

    return refundInfo;
  }

  @Override
  public void updateRefund(String refundNo,String tradeNo, String content, String refundStatus) {

    // 根据退款单编号修改退款信息
    LambdaQueryWrapper<RefundInfo> lqw = new LambdaQueryWrapper<>();
    lqw.eq(RefundInfo::getRefundNo, refundNo);

    // 修改字段
    RefundInfo refundInfo = new RefundInfo();
    // 更新状态
    refundInfo.setRefundStatus(refundStatus);
    // 支付宝单号
    refundInfo.setRefundId(tradeNo);
    // 更新返回参数
    refundInfo.setContentReturn(content);

    refundInfoDao.update(refundInfo, lqw);
  }

    @Override
    public IPage<RefundInfo> queryAllRefundInfo(Integer currentPage, Integer pageSize) {
      IPage page = new Page<>(currentPage, pageSize);
      refundInfoDao.selectPage(page,null);
      return page;
    }

  @Override
  public IPage<RefundInfo> conQueryAllRefundInfo(Integer currentPage, Integer pageSize, RefundInfo refundInfo) {
    IPage page = new Page<>(currentPage, pageSize);
    LambdaQueryWrapper<RefundInfo> lwq=new LambdaQueryWrapper<>();
    lwq.eq(!Objects.isNull(refundInfo.getOrderNo()),RefundInfo::getOrderNo,refundInfo.getOrderNo());
    lwq.eq(!Objects.isNull(refundInfo.getRefundNo()),RefundInfo::getRefundNo,refundInfo.getRefundNo());
    lwq.eq(!Objects.isNull(refundInfo.getRefundId()),RefundInfo::getRefundId,refundInfo.getRefundId());
    lwq.eq(!Objects.isNull(refundInfo.getRefund()),RefundInfo::getRefund,refundInfo.getRefund());
    lwq.eq(!Objects.isNull(refundInfo.getRefundStatus()),RefundInfo::getRefundStatus,refundInfo.getRefundStatus());
    refundInfoDao.selectPage(page,lwq);
    return page;
  }
}
